﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tizer.Core;
using Tizer.Core.DTO;
using Tizer.Core.Enums;
using Tizer.Core.Repository;
using Tizer.Utilities;

namespace Tizer.Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
                using (var accountRepository = new AccountRepository())
                {
                    Account accountAdmin = accountRepository.GetByLogin("admin");
                    if (accountAdmin == null)
                    {
                        accountRepository.Create("admin", "admin", AccountType.Administrator);
                    }
                    
                    Account accountUser = accountRepository.GetByLogin("user");
                    if (accountUser == null)
                    {
                        accountRepository.Create("user", "user", AccountType.Seller);
                    }

                    accountRepository.SaveChanges();
                }
        }

        protected void Authenticate(object sender, AuthenticateEventArgs e)
        {
            if (!string.IsNullOrEmpty(loginForm.UserName) && !string.IsNullOrEmpty(loginForm.Password))
            {
                if (Membership.ValidateUser(loginForm.UserName, loginForm.Password))
                {
                    using (var accountRepository = new AccountRepository())
                    {
                        Account account = accountRepository.GetByLogin(loginForm.UserName);
                        if (account != null)
                        {
                            var loggedAccount = new LoggedAccount
                                                    {
                                                        AccountType = account.AccountType,
                                                        ID = account.ID,
                                                        Login = account.Login
                                                    };
                            if (loginForm.RememberMeSet)
                            {
                                CookieHelper.Set(Response, "tizerUser", loginForm.UserName, DateTime.Now.AddMonths(2));
                                CookieHelper.Set(Response, "tizerRemember", "true");
                            }

                            HttpContext.Current.Session[WebConfigurationManager.AppSettings["LoggedAccount"]] = loggedAccount;

                            if (Roles.IsUserInRole(loginForm.UserName, AccountType.Administrator.ConvertToString()))
                            {
                                loginForm.DestinationPageUrl = "~/Backoffice/Default.aspx";
                                FormsAuthentication.RedirectFromLoginPage(loginForm.UserName, loginForm.RememberMeSet);
                                Response.Redirect("~/Backoffice/Default.aspx");
                            }
                            else
                            {
                                FormsAuthentication.RedirectFromLoginPage(loginForm.UserName, loginForm.RememberMeSet);
                            }
                        }
                    }

                }
            }
        }
    }
}